from django.shortcuts import render,HttpResponseRedirect,HttpResponse
from movie_app import movie_tool
from django.views.decorators.csrf import csrf_exempt
from . import models
from django.core import serializers
import json
from movie_app import spider_tool
# Create your views here.
#电影主界面
def index(request):
    pass
    return render(request,'movie.html')

#电影详情页
def tomovieall(request,name,time):
    #根据名字，上映时间到数据库查找
    film = movie_tool.search_film(name,time)
    if film :
        content = {}
        content["moviename"] = film.name
        content['movieimg'] = film.main_img
        filminfo = film.movie_info
        list = filminfo.split(',')
        content["moviedirector"] = list[1]
        act = film.mainact[1:]
        act = act.replace(',','/')
        content['mainact'] = act
        m_type = film.type[1:]
        m_type = m_type.replace(',','/')
        content['movtype'] = m_type
        content['movietime'] = list[2]
        show_time = film.show_time.strftime('%Y-%m-%d')
        content['mov_showtime'] = show_time
        content['mov_introduce'] = film.introduce
        content['mov_score'] = film.score
        imglist = film.mediadir.split(',')
        #img_line = '<div class="col-2"><div style="background-image:url('++');background-repeat: no-repeat;background-position:center center;background-size: cover;width:165px;height:105px;"></div></div>'
        content['mov_media'] = imglist[1]
        newlist = []
        linklist = []
        news = models.Movie_news.objects.filter(belong=film.id)
        for item in news:
            newlist.append(item.introduce)
            linklist.append(item.linkaddr)
        newsalllist = ''
        for i in range(len(newlist)):
            line = '<a class="alert alert-primary" role="alert" href="'+linklist[i]+'" style="cursor:default;width: 100%;color: firebrick;">'+newlist[i]+'</a>'
            newsalllist = newsalllist +line
        content["mov_new"] = newsalllist
        comment = models.Movie_comment.objects.filter(belong=film.id)
        author_list=[]
        content_list = []
        likecount = []
        for item in comment:
            author_list.append(item.author)
            content_list.append(item.content)
            likecount.append(item.likecount)
        commentall = ''
        for i in range(len(comment)):
            commentline = '<div class="card" style="width: 100%;"><div class="card-body"><h6 class="card-subtitle mb-2 text-muted">' + author_list[i]+'</h6><p class="card-text">'+content_list[i]+'</p><a  class="card-link" onclick="getlike(this)" style="cursor: default;"><i class="zi zi_digg" zico="点赞">点赞</i></a><span>'+str(likecount[i])+'</span><a href="#" class="card-link"><i class="zi zi_commentalt" zico="评论列表">评论</i></a><span>0</span></div></div>'
            commentall = commentall +commentline
        content["mov_comment"] = commentall
        return render(request,'detail.html',content)
    else:
        return render(request,'404.html')


#爬虫程序
def spider(request):
    spider_tool.spider_main()
    return render(request,'detail.html')

#页面数据查询
@csrf_exempt
def index_get(request):
    if request.method == 'POST':
        pagenum = request.POST.get('pagenum')
        end = int(pagenum) * 12
        first = end - 12
        all = models.Movie_info.objects.filter().order_by('id')[first:end]
        json_data = serializers.serialize('python',all) #序列化查询出的数据,转成json数据
        list = []
        i = 1
        for item in json_data:
            linelist = [item['fields']['name'],item['fields']['show_time'].strftime('%Y-%m-%d'),item['fields']['mainact'],item['fields']['main_img']]
            list.append(linelist)
        #keys2 = ['p1','p2','p3','p4','p5','p6','p7','p8','p9','p10','p11','p12']
        #dict2 = dict(zip(keys2,list))
        result = json.dumps(list,ensure_ascii=False)
        return HttpResponse(result)

